# CMakeLists files in this project can
# refer to the root source directory of the project as ${HELLO_SOURCE_DIR} and
# to the root binary directory of the project as ${HELLO_BINARY_DIR}.
cmake_minimum_required (VERSION 2.8.11)
project (lmdb)

# Recurse into the "Hello" and "Demo" subdirectories. This does not actually
# cause another cmake executable to run. The same process will walk through
# the project's entire directory structure.
#add_subdirectory (Hello)
#add_subdirectory (Demo)

# Create a library called "Hello" which includes the source file "hello.cxx".
# The extension is already found. Any number of sources could be listed here.
#add_library (Hello hello.cxx)

# Make sure the compiler can find include files for our Hello library
# when other libraries or executables link to Hello
#target_include_directories (Hello PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
include_directories (AFTER ${CMAKE_CURRENT_SOURCE_DIR})
# Add executable called "helloDemo" that is built from the source files
# "demo.cxx" and "demo_b.cxx". The extensions are automatically found.
#add_executable (helloDemo demo.cxx demo_b.cxx)

# Link the executable to the Hello library. Since the Hello library has
# public include directories we will use those link directories when building
# helloDemo
#target_link_libraries (helloDemo LINK_PUBLIC Hello)
if (WIN32)
	set( GETOPT_SRC getopt.c)
endif()

add_library(lmdb mdb.c midl.c ${GETOPT_SRC} )

add_executable(mdb_stat mdb_stat.c)
target_link_libraries(mdb_stat LINK_PUBLIC lmdb)
add_executable(mdb_copy mdb_copy.c)
target_link_libraries(mdb_copy LINK_PUBLIC lmdb)
add_executable(mdb_dump mdb_dump.c)
target_link_libraries(mdb_dump LINK_PUBLIC lmdb)
add_executable(mdb_load mdb_load.c)
target_link_libraries(mdb_load LINK_PUBLIC lmdb)
add_executable(mdb_drop mdb_drop.c)
target_link_libraries(mdb_drop LINK_PUBLIC lmdb)

target_include_directories(lmdb
                           INTERFACE
                            $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
                            $<INSTALL_INTERFACE:include>
                           )

install(TARGETS mdb_stat mdb_copy mdb_dump mdb_load mdb_drop DESTINATION bin)
install(TARGETS lmdb
          EXPORT liblmdbTargets  
          DESTINATION lib)
install(FILES lmdb.h DESTINATION include)

install(EXPORT liblmdbTargets 
  FILE lmdbTargets.cmake
  DESTINATION lib/cmake/lmdb
)
include(CMakePackageConfigHelpers)

set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR}
    CACHE PATH "Location of header files" )
set(LIB_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}
    CACHE PATH "Location of configuration files" )

# generate the config file that includes the exports
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
  "${CMAKE_CURRENT_BINARY_DIR}/lmdbConfig.cmake"
  INSTALL_DESTINATION "lib/cmake/lmdb"
   PATH_VARS LIB_INSTALL_DIR  INCLUDE_INSTALL_DIR 
  NO_SET_AND_CHECK_MACRO
  NO_CHECK_REQUIRED_COMPONENTS_MACRO
  )
set(lmdb_VERSION_MAJOR 1)
set(lmdb_VERSION_MINOR 0)
write_basic_package_version_file(
  "${CMAKE_CURRENT_BINARY_DIR}/lmdbConfigVersion.cmake"
  VERSION "${lmdb_VERSION_MAJOR}.${lmdb_VERSION_MINOR}"
  COMPATIBILITY AnyNewerVersion
)
install(FILES
  ${CMAKE_CURRENT_BINARY_DIR}/lmdbConfig.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/lmdbConfigVersion.cmake
  DESTINATION lib/cmake/lmdb
  )